<script>
	import {
		userInfo
	} from './pages/name/js/api.js'
	import {
		noticePage
	} from './pages/message/js/api.js'
	export default {
		onLaunch: function() {
			const systemInfo = uni.getSystemInfoSync()
			this.globalData.statusBarHeight = systemInfo.statusBarHeight || 0

			// 微信小程序胶囊处理
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			this.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo
				.height
			this.globalData.menuRight = systemInfo.windowWidth - menuButtonInfo.right

			this.agetInfoData()
			// wx.setTabBarBadge({
			//   index: 2, // tabBar 的索引，从 0 开始
			//   text: '' // 显示的文本，若设为 '' 则显示纯红点
			// })
		},

		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			setIsLogins(type) {
				if (type == 'loginOut') return this.globalData.isLogins = false
				this.globalData.isLogins = true
			},
			setPhoneNumber(prams) {
				this.globalData.phoneNumber = prams
			},

			setUserType(userType) {
				this.globalData.userType = userType
			},
			async agetInfoData() {
				let res = await userInfo()
				// console.log('res====',res)
				if (res.data.code == 0) {
					let data = res.data.data
					this.setIsLogins()
					this.setPhoneNumber(data.phone)
					// console.log('data====',data)
					this.globalData.userType = data.userType
				}
			},
			setHousingList(list) {
				this.globalData.housingList = list
			},
			setRealNameFlag(value) {
				this.globalData.realNameFlag = value
			},
			//设置红点
			async setTabBarBadge() {
				if (!this.globalData.isLogins) return
				try {
					// 发起通知列表请求
					const {
						data
					} = await noticePage({
						pageNo: 1,
						pageSize: 10
					});
					console.log(data)
					// 检查响应状态
					if (data.code != 0) {
						// console.error('获取通知列表失败:', data.message);
						return;
					}
					// 统计未读消息数量
					const unreadCount = data.data?.list?.reduce((count, item) => {
						return item.status === 0 ? count + 1 : count;
					}, 0) || 0;
					console.log('unreadCount', unreadCount)
					// 更新TabBar红点
					if (unreadCount > 0) {
						wx.setTabBarBadge({
							index: 2,
							text: String(unreadCount) // 确保text是字符串类型
						});
					} else {
						wx.removeTabBarBadge({
							index: 2
						});
					}
				} catch (error) {
					console.error('设置TabBar红点时出错:', error);
				}
			}
		},
		globalData: {
			statusBarHeight: 0,
			navBarHeight: 44,
			menuRight: 0,
			userType: 1,
			isLogins: false, //用户是否登录
			userInfo: {}, //用户信息
			phoneNumber: '', //用户手机号
			housingList: [], //房屋情况列表
			realNameFlag: 0
		}
	}
</script>

<style>
	/*每个页面公共css */
	.dis {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	button::after {
		border: none;
		/* 去除默认边框 */
		border-radius: 0;
		/* 确保伪元素也没有圆角 */
	}

	.required::before {
		content: '*';
		color: #f56c6c;
		/* 红色 */
		margin-right: 4rpx;
	}

	.norequired::before {
		content: '*';
		color: #FFF;
		margin-right: 4rpx;
	}
</style>